package com.wzw.basics.concurrent.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Wangzhiwen
 */
public class BlockingQueueDemo {

    public static void main(String[] args) {

        System.out.println("抛出异常组：add remove element");

        // 指定容量数
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);

        // 抛出异常组 add remove element
        blockingQueue.add(1);
        blockingQueue.add(2);
        blockingQueue.add(3);

        try {
            blockingQueue.add(4);
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println(blockingQueue.element());

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

        try {
            Integer remove4 = blockingQueue.remove();
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("=====================================");

        System.out.println();

        System.out.println("返回特殊值组：offer poll peek");

        // 返回特殊值组 offer poll peek
        BlockingQueue<Integer> blockingQueue2 = new ArrayBlockingQueue<>(2);

        // 存入成功返回true 失败返回 false
        System.out.println(blockingQueue2.offer(1));
        System.out.println(blockingQueue2.offer(2));
        System.out.println(blockingQueue2.offer(3));

        System.out.println(blockingQueue2.offer(4));

        System.out.println(blockingQueue2.peek());

        // 消费 成功返回对象，失败返回null
        System.out.println(blockingQueue2.poll());
        System.out.println(blockingQueue2.poll());
        System.out.println(blockingQueue2.poll());

        System.out.println(blockingQueue2.poll());

        System.out.println("=====================================");

        System.out.println();

        System.out.println("阻塞组：put take");

        // 阻塞组  put take 无检查
        BlockingQueue<Integer> blockingQueue3 = new ArrayBlockingQueue<>(3);

        try {
            blockingQueue3.put(1);
            blockingQueue3.put(2);
            blockingQueue3.put(3);
            blockingQueue3.put(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.out.println(blockingQueue3.take());
            System.out.println(blockingQueue3.take());
            System.out.println(blockingQueue3.take());
            System.out.println(blockingQueue3.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("=====================================");

        System.out.println();

        System.out.println("超时组：offer poll");

        // 超时组 offer poll 无检查
        BlockingQueue<Integer> blockingQueue4 = new ArrayBlockingQueue<>(3);

        try {
            blockingQueue4.offer(1,2, TimeUnit.SECONDS);
            blockingQueue4.offer(2,2, TimeUnit.SECONDS);
            blockingQueue4.offer(3,2, TimeUnit.SECONDS);
            blockingQueue4.offer(4,2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.out.println(blockingQueue4.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue4.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue4.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue4.poll(2, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
